package system;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Clock;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import bean.Questionnaire;
import DB.DB_connection_pool;

public class Questionnaire_deal extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		JSONObject jsonObj = new JSONObject();
		
		String questionnaire_theme = request.getParameter("questionnaire_theme");
		String questionnaire_content = request.getParameter("questionnaire_content");
		
		Clock clock = Clock.systemUTC();
		String questionnaire_id = String.valueOf((int)(Math.random()*900000)+100000)+clock.millis();  //反馈意见编号;
		Timestamp questionnaire_date = new Timestamp(clock.millis());     //当前时间
		
		String insert_sql = "insert into system_question_paper(questionnaire_id, questionnaire_date, questionnaire_theme, questionnaire_content) values(?,?,?,?)";
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_insert = conn.prepareStatement(insert_sql);
			ps_insert.setString(1, questionnaire_id);
			ps_insert.setTimestamp(2, questionnaire_date);
			ps_insert.setString(3, questionnaire_theme);
			ps_insert.setString(4, questionnaire_content);
			
			if(ps_insert.executeUpdate()!=0){
				
				String select_sql = "select * from system_question_paper where questionnaire_release = 1";
				
				PreparedStatement ps_select = conn.prepareStatement(select_sql);
				ResultSet rs = ps_select.executeQuery();
				
				List<Questionnaire> questionnaires = new ArrayList<Questionnaire>();
				int questionnaire_count = 0;
				while(rs.next()){
					Questionnaire questionnaire = new Questionnaire();
					
					questionnaire.setQuestionnaire_id(rs.getString("questionnaire_id"));
					questionnaire.setQuestionnaire_date(rs.getTimestamp("questionnaire_date"));
					questionnaire.setQuestionnaire_theme(rs.getString("questionnaire_theme"));
					questionnaire.setQuestionnaire_content(new JSONArray(rs.getString("questionnaire_content")));
					questionnaire.setQuestionnaire_release(rs.getInt("questionnaire_release"));
					
					questionnaires.add(questionnaire);
					questionnaire_count++;
				}
				request.getServletContext().setAttribute("questionnaires", questionnaires);
				request.getServletContext().setAttribute("questionnaire_count", questionnaire_count);
				
				rs.close();
				ps_select.close();
				
				jsonObj.put("result", true);
			}else{
				jsonObj.put("result", false);
			}
			ps_insert.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			jsonObj.put("result", false);
			e.printStackTrace();
		}
		out.print(jsonObj.toString());
	}

}
